/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services.implementation;

import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import entity.Users;
import javax.ejb.Remote;
import services.IUsersService;



/**
 *
 * @author smile
 */
@Remote
@Stateless
@LocalBean
public class UsersService extends GenericServices implements IUsersService {
    @PersistenceContext(unitName = "snPU")
    private EntityManager em;

    @Override
    public boolean addUser(Object object) {
        return this.insert(object);
    }

    @Override
    public boolean deleteUser(Object object) {
        return this.delete(object);
    }

    @Override
    public boolean updateUser(Object object) {
        return this.update(object);
    }

    @Override
    public List<Users> getAllUsers() {
        return em.createNamedQuery("Users.findAll").getResultList();
    }

    @Override
    public Users getUserById(Integer id) {
        return em.find(Users.class, id);
    }
    
    public boolean updatePassword (Integer id, String password){
        try {
            em.createNamedQuery("Users.updatePassword")
                    .setParameter("id", id)
                    .setParameter("password", password)
                    .executeUpdate();
            return true;
        }catch(Exception e){
            return false;
        }
    }

    public boolean updateEmail (Integer id, String email){
        try {
            em.createNamedQuery("Users.updateEmail")
                    .setParameter("id", id)
                    .setParameter("email", email)
                    .executeUpdate();
            return true;
        }catch(Exception e){
            return false;
        }
    }
    
            

    public boolean checkCurrentID (Integer id){
        if (this.em.createNamedQuery("Users.findById")
                    .setParameter("id", id)
                    .getResultList().isEmpty()) {
            return false;
        }else {
            return true;
        }   
    }
         
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public List<Users> findByEmail(String email) {
        return this.em.createNamedQuery("Users.findByEmail")
                .setParameter("email", email)
                .getResultList();
    }
    
 
    
}
